package com.nbjtjc.safe.service.sys;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.Exception.SafeException;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.mapper.sys.SysDutyMapper;
import com.nbjtjc.safe.mapper.sys.SysDutyTypeMapper;
import com.nbjtjc.safe.model.sys.SysDuty;
import com.nbjtjc.safe.util.DateUtils;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @auther guqianbin
 * @date 2018/7/23 9:59
 **/
@Service
public class SysDutyService {

    @Autowired
    private SysDutyMapper sysDutyMapper;

    @Autowired
    private SysDutyTypeMapper sysDutyTypeMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    public SysDuty update(SysDuty sysDuty){
        sysDuty.setWriteTime(new Date());
        sysDuty.setWriteUserId(MyShiroUtils.getSessionUserId());
        sysDutyMapper.updateByPrimaryKeySelective(sysDuty);
        return sysDuty;
    }

    public SysDuty add(SysDuty sysDuty){
        sysDutyMapper.insertSelective(sysDuty);
        return sysDuty;
    }

    public PageInfo find(SysDuty sysDuty){
        PageHelper.startPage(sysDuty.getPageCurrent(),sysDuty.getPageSize());
        int year=0;
        if(ObjectUtil.isNull(sysDuty.getYear())){
            year= DateUtil.thisYear();
        }else{
            year=sysDuty.getYear();
        }
        List<Date> dates=DateUtils.getYearRange(year);
        sysDuty.setStartDate(dates.get(0));
        sysDuty.setEndDate(dates.get(1));
        List<SysDuty> sysDuties= sysDutyMapper.search(sysDuty);
        return new PageInfo(sysDuties);
    }

    public  Map<Integer,List<SysDuty>> getPublicDuty(SysDuty sysDuty){
        if(ObjectUtil.isNull(sysDuty)||ObjectUtil.isNull(sysDuty.getDutyPublicId())){
            throw new SafeException("dutyPublicId不能为空");
        }
        Map<Integer,List<SysDuty>> companyDutyMap=new HashMap<>();

        List<SysDuty> sysDuties=sysDutyMapper.findByDutyPublicId(sysDuty.getDutyPublicId());
        if(CollectionUtil.isEmpty(sysDuties)){
            return companyDutyMap;
        }

        sysDuties.forEach(duty->{
            if(companyDutyMap.containsKey(duty.getCompanyId())){
                companyDutyMap.get(duty.getCompanyId()).add(duty);
            }else{
                companyDutyMap.put(duty.getCompanyId(),new ArrayList<>());
                companyDutyMap.get(duty.getCompanyId()).add(duty);
            }
        });
        return companyDutyMap;
    }


    public Map<Integer,Map<String,Object>> getDutyByType(SysDuty sysDuty,int year){
        if(ObjectUtil.isNull(sysDuty)||ObjectUtil.isNull(sysDuty.getDutyTypeCode())){
            throw new SafeException("dutyTypeCode 不能为空或者查询对象不能为空");
        }
        List<Date> dates= DateUtils.getYearRange(year);
        sysDuty.setStartDate(DateUtils.plusDate(dates.get(0),4));
        sysDuty.setEndDate(DateUtils.plusDate(dates.get(1),4));
        if(sysDuty.getCompanyId().equals(0)){
            sysDuty.setCompanyId(MyShiroUtils.getCompanyId());
        }
        List<Integer> subCompanyIds= sysDeptMapper.getSubCompanyId(sysDuty.getCompanyId(), Constant.SysDeptType.COMPANY.getValue());
        if(CollectionUtil.isEmpty(subCompanyIds)){
            subCompanyIds=new ArrayList<>();
        }
        subCompanyIds.add(sysDuty.getCompanyId());
        subCompanyIds.addAll(sysDeptMapper
                .getSubCompanyId(MyShiroUtils.getCompanyId()
                        ,Constant.SysDeptType.COMPANY.getValue()));
        Map<Integer,Map<String,Object>> dutyMap=new HashMap<>();
        subCompanyIds.forEach(subCompanyId->{
            Map<String,Object> companyDutyMap=new HashMap<>();
            companyDutyMap.put("companyName",sysDeptMapper.findNameById(subCompanyId));
            companyDutyMap.put("dutyList",new ArrayList<>());
            dutyMap.put(subCompanyId,companyDutyMap);
        });
        List<SysDuty> sysDutyList=sysDutyMapper.findByCompanyIds(subCompanyIds,sysDuty);
        if(CollectionUtil.isNotEmpty(sysDutyList)){
            sysDutyList.forEach(duty->{
                if(dutyMap.containsKey(duty.getCompanyId())){
                    List<SysDuty> sysDuties= (List<SysDuty>) dutyMap.get(duty.getCompanyId()).get("dutyList");
                    sysDuties.add(duty);
                }
            });
        }
        return dutyMap;
    }
}
